<template>
  <div class="container">
    <!-- 卡片区域 -->
    <el-card>
      <!-- form表单 -->
      <el-form
        ref="restFormRef"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        :rules="rules"
      >
        <el-form-item label="标签名称:" prop="tags">
          <el-input
            v-model="formInline.tags"
            placeholder="请输入"
            class="label"
          ></el-input>
        </el-form-item>

        <el-form-item label="省:" prop="province">
          <el-select
            v-model="formInline.province"
            placeholder="请选择"
            @change="selextChange"
            class="province"
          >
            <el-option
              v-for="(item, index) in provinces"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="市:" prop="city">
          <el-select
            v-model="formInline.city"
            placeholder="请选择"
            class="province"
          >
            <el-option
              :label="item"
              :value="item"
              v-for="(item, index) in citys"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="企业简称:" prop="shortName">
          <el-input
            v-model="formInline.shortName"
            placeholder=""
            class="enterprise"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态:" prop="state">
          <el-select
            v-model="formInline.state"
            placeholder="请选择"
            class="province"
          >
            <el-option label="禁用" value="0"></el-option>
            <el-option label="启用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <br />
        <el-row>
          <el-col :span="17">
            <el-button
              @click="chaxun"
              type="primary"
              icon="el-icon-search"
              size="mini"
              >查询</el-button
            >
            <el-button @click="restformRef">
              重置
            </el-button>
          </el-col>
          <el-col :span="7">
            <el-button type="primary" class="el-icon-edit" @click="adduser"
              >新增用户</el-button
            >
          </el-col>
        </el-row>
      </el-form>
      <!-- form表单 -->

      <div class="box"></div>

      <!-- 提示 -->
      <el-alert
        :title="`共${this.counts}条记录`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- /提示 -->

      <!-- 表格区域 -->
      <el-table style="width: 100%" :data="list" border>
        <el-table-column prop="id" label="序号" width="80"> </el-table-column>
        <el-table-column prop="number" label="企业编号"> </el-table-column>
        <el-table-column prop="shortName" label="企业简称" width="80">
        </el-table-column>
        <el-table-column prop="tags" label="标签" width="80"> </el-table-column>
        <el-table-column prop="creatorID" label="创建者" width="80">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template slot-scope="scope">
            {{ scope.row.addDate | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="备注"> </el-table-column>

        <el-table-column label="状态" width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.state === 1">启用</span>
            <span v-else>禁用</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="210">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button type="primary" size="mini" @click="editor(scope.row)">
              编辑
            </el-button>
            <!-- /编辑按钮 -->
            <!-- 禁用按钮 -->
            <el-button
              type="primary"
              size="mini"
              @click="disabledstate(scope.row)"
            >
              <span v-if="scope.row.state === 1">禁用</span>
              <span v-else>启用</span>
            </el-button>
            <!-- /禁用按钮 -->
            <el-button @click="remove(scope.row)" type="danger" size="mini"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- /表格区域 -->

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="parameter.page"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="parameter.pagesize"
        layout=" sizes, prev, pager, next, jumper"
        :total="counts"
      >
      </el-pagination>
      <!-- /分页区域 -->
    </el-card>
    <!-- /卡片区域 -->

    <!-- 编辑用户对话框 -->

    <editor
      :editorVisible="editorVisible"
      @closedialog="editorVisible = false"
      :dialogue="dialogue"
      :companyDetails="companyDetails"
      @closed="editorVisible = false"
      @updatainfo="getinfo"
    ></editor>

    <!-- /编辑用户对话框 -->
  </div>
</template>

<script>
import { list, remove, disabled, detail } from "@/api/hmmm/companys.js";
import editor from "@/module-hmmm/components/companys-edit";
import { provinces, citys } from "@/api/hmmm/citys.js";
export default {
  components: {
    editor
  },
  data() {
    return {
      // 验证规则
      rules: {
        tags: [{ required: true, message: "请输入", trigger: "blur" }],
        shortName: [{ required: true, message: "请输入", trigger: "blur" }],
        province: [{ required: true, message: "请选择", trigger: "blur" }],
        city: [{ required: true, message: "请选择", trigger: "blur" }],
        state: [{ required: true, message: "请选择", trigger: "blur" }]
      },
      // 搜索参数
      formInline: {
        page: 1,
        pagesize: 10,
        tags: null,
        shortName: null,
        state: null,
        province: null,
        city: null
      },
      list: [],
      parameter: {
        page: 1,
        pagesize: 10
      },
      counts: 0,
      provinces: provinces(),
      citys: [],
      editorVisible: false, // 对话框的显示与隐藏
      dialogue: true,
      companyDetails: {}
    };
  },
  methods: {
    // 重置表单
    restformRef() {
      this.$refs.restFormRef.resetFields();
      this.getinfo();
    },
    selextChange(item) {
      this.citys = citys(item);
    },
    // 请求获取所有数据
    async getinfo() {
      try {
        const { data } = await list(this.parameter);
        // console.log(data);
        this.list = data.items;
        this.counts = data.counts;
      } catch (error) {
        this.$message("获取失败");
      }
    },
    handleSizeChange(newsize) {
      this.parameter.pagesize = newsize;

      this.getinfo();
    },
    handleCurrentChange(newnum) {
      this.parameter.page = newnum;

      this.getinfo();
    },
    // 根据id删除对应信息
    async remove(id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmResult !== "confirm") {
        return this.$message.success("已经取消删除");
      }
      const { data } = await remove(id);
      // console.log(data);
      if (!data.success) {
        return this.$message.error("删除失败");
      }
      this.$message.success("删除成功");
      this.getinfo();
    },

    // 添加新用户
    adduser() {
      this.editorVisible = true;
      this.dialogue = false;
    },
    // 根据id编辑用户信息
    async editor(id) {
      this.editorVisible = true;
      this.dialogue = true;
      const { data } = await detail(id);

      this.companyDetails = data;

      if (this.companyDetails.isFamous === 1) {
        this.companyDetails.isFamous = true;
      } else {
        this.companyDetails.isFamous = false;
      }
      // console.log(this.companyDetails);
    },
    // 根据id和state禁用
    async disabledstate(data) {
      if (data.state === 1) {
        await disabled({
          id: data.id,
          state: 0
        });
      } else {
        await disabled({
          id: data.id,
          state: 1
        });
      }
      this.getinfo();
    },
    // 查询
    async chaxun() {
      const { data } = await list(this.formInline);
      this.list = data.items;
      this.counts = data.counts;
    }
  },
  created() {
    // 页面加载渲染页面
    this.getinfo();
  }
};
</script>

<style scoped lang="less">
.container {
  padding: 15px;
}
.label {
  width: 150px;
  font-size: 12px;
}
.province {
  width: 200px;
  font-size: 12px;
}
.enterprise {
  width: 150px;
  font-size: 12px;
}
/deep/.el-form-item__label {
  font-size: 12px;
}
.box {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}

.el-pagination {
  margin-top: 15px;
}
</style>